﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow;

namespace VideoCapture
{
    public partial class frmCamList : Form
    {
        private bool DeviceExist = false;
        private FilterInfoCollection videoDevices;

        public string InputDeviceName = "";

        public frmCamList()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnSelectCam.Enabled = true;
        }

        private void frmCamList_Load(object sender, EventArgs e)
        {
            GetCamList();
        }

        private void GetCamList()
        {
            try
            {
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                cboCamList.Items.Clear();
                if (videoDevices.Count == 0)
                    throw new ApplicationException();

                DeviceExist = true;
                foreach (FilterInfo device in videoDevices)
                {
                    cboCamList.Items.Add(device.Name);
                }
                cboCamList.SelectedIndex = 0; //make dafault to first cam
            }
            catch (ApplicationException)
            {
                DeviceExist = false;
                btnSelectCam.Enabled = false;
            }
        }

        private void btnSelectCam_Click(object sender, EventArgs e)
        {
            InputDeviceName = videoDevices[cboCamList.SelectedIndex].MonikerString;
            this.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            GetCamList();
        }
    }
}
